<template>
  <div class="app-container">
    <template>
      <div class="hidden-md-and-up">
        <el-alert title="如若不适应手机端操作，建议使用电脑操作" type="warning" />
      </div>
    </template>
    <div class="table-container">
      <div style="padding-bottom:20px; display: flex;">
        <el-button type="primary" size="medium" plain @click="addthesis">添加</el-button>
        <div style="width: 200px; margin-left: 20px;">
          <el-input v-model="search" size="medium" placeholder="输入关键字搜索" />
        </div>
      </div>
      <el-table
        v-loading="paperListLoading"
        :data="paperList.filter(data => !search || data.projectCateName.toLowerCase().includes(search.toLowerCase()))"
        element-loading-text="Loading"
        border
        fit
      >
        <el-table-column type="selection" align="center" width="55" />
        <!-- <el-table-column align="center" label="序号" width="55">
          <template slot-scope="scope">
            {{ scope.$index+1 }}
          </template>
        </el-table-column> -->
        <el-table-column align="left" label="名称" width="500">
          <template slot-scope="scope">
            {{ scope.row.projectCateName }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="200">
          <template slot-scope="scope">
            <el-popover
              :ref="`popoverdelete-${scope.$index}`"
              placement="top"
              width="160"
            >
              <p>确认删除本行吗？该操作不可撤销</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="scope._self.$refs[`popoverdelete-${scope.$index}`].doClose()">取消</el-button>
                <el-button type="primary" size="mini" @click="delete_thesis(scope.$index, scope.row)">确定</el-button>
              </div>
            </el-popover>
            <el-button
              size="mini"
              @click="open_thesis_info_dialog(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              v-popover="`popoverdelete-${scope.$index}`"
              size="mini"
              type="danger"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="添加" :visible.sync="thesis_add_dialog" width="75%" :close-on-click-modal="false">
        <el-form ref="thesis_add_form1" :model="thesis_add_form1" label-position="top" label-width="160px" autocomplete="off">
          <el-row>
            <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
              <el-form-item label="名称" prop="projectCateName">
                <el-input v-model="thesis_add_form1.projectCateName" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-popover
            ref="dialog1"
            v-model="visible1"
            placement="top"
            width="160"
          >
            <p>关闭后，已填写的内容会丢失，建议保存为草稿，确定要关闭吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="visible1 = false">取消</el-button>
              <el-button type="primary" size="mini" @click="visible1 = false;thesis_add_dialog = false;">确定</el-button>
            </div>
          </el-popover>
          <el-button v-popover:dialog1>关 闭</el-button>
          <el-button type="primary" @click="save_add_form1(thesis_add_form1)">保 存</el-button>
        </div>
      </el-dialog>
      <el-dialog title="修改" :visible.sync="thesis_info_dialog" width="75%" :close-on-click-modal="false">
        <el-form ref="thesis_add_form2" :model="thesis_add_form2" label-position="top" label-width="160px" autocomplete="off">
          <el-row>
            <el-col :xs="20" :sm="20" :md="10" :lg="10" :xl="8">
              <el-form-item label="名称" prop="projectCateName">
                <el-input v-model="thesis_add_form2.projectCateName" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-popover
            ref="dialog2"
            v-model="visible2"
            placement="top"
            width="160"
          >
            <p>关闭后，已填写的内容会丢失，建议保存为草稿，确定要关闭吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="visible2 = false">取消</el-button>
              <el-button type="primary" size="mini" @click="visible2 = false;thesis_info_dialog = false;">确定</el-button>
            </div>
          </el-popover>
          <el-button v-popover:dialog2>关 闭</el-button>
          <el-button type="primary" @click="save_add_form2(thesis_add_form2)">保 存</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getProjectCate, addProjectcate, updateProjectcate, deleteProjectcate } from '@/api/system'
import { Message } from 'element-ui'
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      search: '',
      thesis_add_dialog: false,
      thesis_info_dialog: false,
      visible1: false,
      visible2: false,
      thesis_add_form1: {
        projectCateID: '',
        projectCateName: ''
      },
      thesis_add_form2: {
        projectCateID: '',
        projectCateName: ''
      },
      paperList: null,
      paperListLoading: false
    }
  },
  computed: {
    ...mapGetters([
      'uid',
      'roles',
      'name'
    ])
  },
  created() {
    this.fetchPaperData()
  },
  methods: {
    fetchPaperData() {
      this.paperListLoading = true
      getProjectCate().then(response => {
        if (response.code === 200) {
          this.paperList = response.data
          this.paperListLoading = false
        }
      })
    },
    filterTag(value, row) {
      return row.status === value
    },
    resetForm(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
    },
    addthesis() {
      this.thesis_add_dialog = true
      this.resetForm('thesis_add_form1')
    },
    save_add_form1(formdata) {
      addProjectcate(formdata).then(response => {
        if (response.code === 200) {
          Message.success('保存成功')
          this.thesis_add_dialog = false
          this.fetchPaperData()
        }
      })
    },
    open_thesis_info_dialog(row) {
      this.thesis_info_dialog = true
      this.resetForm('thesis_add_form2')
      this.thesis_add_form2 = JSON.parse(JSON.stringify(row))
    },
    save_add_form2(formdata) {
      updateProjectcate(formdata).then(response => {
        if (response.code === 200) {
          Message.success('保存成功')
          this.thesis_info_dialog = false
          this.fetchPaperData()
        }
      })
    },
    delete_thesis(index, row) {
      this.$refs[`popoverdelete-${index}`].doClose()
      deleteProjectcate(row.projectCateID).then(response => {
        if (response.code === 200) {
          Message.success('删除成功')
          this.fetchPaperData()
        }
      })
    }
  }
}
</script>

<style lang="scss">
.el-form{
  margin-left: 40px;
  .el-input,.el-select{
    width: 300px;
  }
}
.el-tag + .el-tag {
  margin: 10px 10px;
}
.margin-top {
  margin-top: 50px;
}
@media screen and (max-width:600px) {
  .el-dialog{
    width: 100% !important;
  }
  .el-form{
    margin-left: 25px;
  }
}
</style>
